package com.cxfpro;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @author: xuanfei.Chen
 * @Date: 2018/12/6 10:06
 * @Description: 用网关拦截参数
 */
@Component
public class Myfilter extends ZuulFilter {

  private static Logger log = LoggerFactory.getLogger(Myfilter.class);

  @Override
  public String filterType(){
    return "pre";
  }

  @Override
  public int filterOrder() {
    return 0;
  }

  @Override
  public boolean shouldFilter(){
    return true;
  }

  @Override
  public Object run(){
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();
    log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));
    Object accessToken = request.getParameter("token");
    if(accessToken != null){
      return null;
    }
    log.warn("token is empty");
    ctx.setSendZuulResponse(false);
    ctx.setResponseStatusCode(401);
    try{
      ctx.getResponse().getWriter().write("token is empty");
    }catch (Exception e){

    }
    return null;
  }

}
